package scau.jump.securitytrace.service.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.securitytrace.model.Product;

import scau.jump.securitytrace.service.ProductService;

public class ProductServiceImpl extends HibernateDaoSupport implements ProductService {

	public long getTotalCount() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Product.class);
		criteria.setProjection(Projections.count("id"));
		return (Long) criteria.getExecutableCriteria(getSession()).uniqueResult();
	}

	public boolean exist(long id) {
		Product obj = this.getHibernateTemplate().get(Product.class, id);
		return obj != null;
	}

	public Product getById(long id) {
		return getHibernateTemplate().get(Product.class, id);
	}
	
	@SuppressWarnings("unchecked")
	public List<Product> getList() {
		return getSession().createCriteria(Product.class).list();
	}

	@SuppressWarnings("unchecked")
	public List<Product> getListByPage(int start, int pageSize) {
		return getSession().createCriteria(Product.class)
					.setFirstResult(start).setMaxResults(pageSize).list();
	}

	public void deleteById(long id) {
		getHibernateTemplate().delete(getHibernateTemplate().get(Product.class, id));
	}

	public void add(Product obj) {
		getHibernateTemplate().save(obj);
	}

	public void update(Product obj) {
		getHibernateTemplate().update(obj);
	}

}
